/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package it.apuliait.service.persistence;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import it.apuliait.NoSuchDatiTrasportoException;

import it.apuliait.model.DatiTrasporto;
import it.apuliait.model.impl.DatiTrasportoImpl;
import it.apuliait.model.impl.DatiTrasportoModelImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the dati trasporto service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Donatello
 * @see DatiTrasportoPersistence
 * @see DatiTrasportoUtil
 * @generated
 */
public class DatiTrasportoPersistenceImpl extends BasePersistenceImpl<DatiTrasporto>
	implements DatiTrasportoPersistence {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never modify or reference this class directly. Always use {@link DatiTrasportoUtil} to access the dati trasporto persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
	 */
	public static final String FINDER_CLASS_NAME_ENTITY = DatiTrasportoImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List1";
	public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List2";
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
			DatiTrasportoModelImpl.FINDER_CACHE_ENABLED,
			DatiTrasportoImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
			"findAll", new String[0]);
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
			DatiTrasportoModelImpl.FINDER_CACHE_ENABLED,
			DatiTrasportoImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
			DatiTrasportoModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
	public static final FinderPath FINDER_PATH_FETCH_BY_DATOTRASPORTO = new FinderPath(DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
			DatiTrasportoModelImpl.FINDER_CACHE_ENABLED,
			DatiTrasportoImpl.class, FINDER_CLASS_NAME_ENTITY,
			"fetchByDatoTrasporto", new String[] { Long.class.getName() },
			DatiTrasportoModelImpl.DOCUMENTOIDFK_COLUMN_BITMASK);
	public static final FinderPath FINDER_PATH_COUNT_BY_DATOTRASPORTO = new FinderPath(DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
			DatiTrasportoModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByDatoTrasporto",
			new String[] { Long.class.getName() });

	/**
	 * Returns the dati trasporto where documentoIDFK = &#63; or throws a {@link it.apuliait.NoSuchDatiTrasportoException} if it could not be found.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @return the matching dati trasporto
	 * @throws it.apuliait.NoSuchDatiTrasportoException if a matching dati trasporto could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public DatiTrasporto findByDatoTrasporto(long documentoIDFK)
		throws NoSuchDatiTrasportoException, SystemException {
		DatiTrasporto datiTrasporto = fetchByDatoTrasporto(documentoIDFK);

		if (datiTrasporto == null) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("documentoIDFK=");
			msg.append(documentoIDFK);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			if (_log.isWarnEnabled()) {
				_log.warn(msg.toString());
			}

			throw new NoSuchDatiTrasportoException(msg.toString());
		}

		return datiTrasporto;
	}

	/**
	 * Returns the dati trasporto where documentoIDFK = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @return the matching dati trasporto, or <code>null</code> if a matching dati trasporto could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public DatiTrasporto fetchByDatoTrasporto(long documentoIDFK)
		throws SystemException {
		return fetchByDatoTrasporto(documentoIDFK, true);
	}

	/**
	 * Returns the dati trasporto where documentoIDFK = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param retrieveFromCache whether to use the finder cache
	 * @return the matching dati trasporto, or <code>null</code> if a matching dati trasporto could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public DatiTrasporto fetchByDatoTrasporto(long documentoIDFK,
		boolean retrieveFromCache) throws SystemException {
		Object[] finderArgs = new Object[] { documentoIDFK };

		Object result = null;

		if (retrieveFromCache) {
			result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_DATOTRASPORTO,
					finderArgs, this);
		}

		if (result instanceof DatiTrasporto) {
			DatiTrasporto datiTrasporto = (DatiTrasporto)result;

			if ((documentoIDFK != datiTrasporto.getDocumentoIDFK())) {
				result = null;
			}
		}

		if (result == null) {
			StringBundler query = new StringBundler(3);

			query.append(_SQL_SELECT_DATITRASPORTO_WHERE);

			query.append(_FINDER_COLUMN_DATOTRASPORTO_DOCUMENTOIDFK_2);

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(documentoIDFK);

				List<DatiTrasporto> list = q.list();

				if (list.isEmpty()) {
					FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_DATOTRASPORTO,
						finderArgs, list);
				}
				else {
					if ((list.size() > 1) && _log.isWarnEnabled()) {
						_log.warn(
							"DatiTrasportoPersistenceImpl.fetchByDatoTrasporto(long, boolean) with parameters (" +
							StringUtil.merge(finderArgs) +
							") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
					}

					DatiTrasporto datiTrasporto = list.get(0);

					result = datiTrasporto;

					cacheResult(datiTrasporto);

					if ((datiTrasporto.getDocumentoIDFK() != documentoIDFK)) {
						FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_DATOTRASPORTO,
							finderArgs, datiTrasporto);
					}
				}
			}
			catch (Exception e) {
				FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_DATOTRASPORTO,
					finderArgs);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		if (result instanceof List<?>) {
			return null;
		}
		else {
			return (DatiTrasporto)result;
		}
	}

	/**
	 * Removes the dati trasporto where documentoIDFK = &#63; from the database.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @return the dati trasporto that was removed
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public DatiTrasporto removeByDatoTrasporto(long documentoIDFK)
		throws NoSuchDatiTrasportoException, SystemException {
		DatiTrasporto datiTrasporto = findByDatoTrasporto(documentoIDFK);

		return remove(datiTrasporto);
	}

	/**
	 * Returns the number of dati trasportos where documentoIDFK = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @return the number of matching dati trasportos
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public int countByDatoTrasporto(long documentoIDFK)
		throws SystemException {
		FinderPath finderPath = FINDER_PATH_COUNT_BY_DATOTRASPORTO;

		Object[] finderArgs = new Object[] { documentoIDFK };

		Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
				this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_DATITRASPORTO_WHERE);

			query.append(_FINDER_COLUMN_DATOTRASPORTO_DOCUMENTOIDFK_2);

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(documentoIDFK);

				count = (Long)q.uniqueResult();

				FinderCacheUtil.putResult(finderPath, finderArgs, count);
			}
			catch (Exception e) {
				FinderCacheUtil.removeResult(finderPath, finderArgs);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return count.intValue();
	}

	private static final String _FINDER_COLUMN_DATOTRASPORTO_DOCUMENTOIDFK_2 = "datiTrasporto.documentoIDFK = ?";

	public DatiTrasportoPersistenceImpl() {
		setModelClass(DatiTrasporto.class);
	}

	/**
	 * Caches the dati trasporto in the entity cache if it is enabled.
	 *
	 * @param datiTrasporto the dati trasporto
	 */
	@Override
	public void cacheResult(DatiTrasporto datiTrasporto) {
		EntityCacheUtil.putResult(DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
			DatiTrasportoImpl.class, datiTrasporto.getPrimaryKey(),
			datiTrasporto);

		FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_DATOTRASPORTO,
			new Object[] { datiTrasporto.getDocumentoIDFK() }, datiTrasporto);

		datiTrasporto.resetOriginalValues();
	}

	/**
	 * Caches the dati trasportos in the entity cache if it is enabled.
	 *
	 * @param datiTrasportos the dati trasportos
	 */
	@Override
	public void cacheResult(List<DatiTrasporto> datiTrasportos) {
		for (DatiTrasporto datiTrasporto : datiTrasportos) {
			if (EntityCacheUtil.getResult(
						DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
						DatiTrasportoImpl.class, datiTrasporto.getPrimaryKey()) == null) {
				cacheResult(datiTrasporto);
			}
			else {
				datiTrasporto.resetOriginalValues();
			}
		}
	}

	/**
	 * Clears the cache for all dati trasportos.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache() {
		if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
			CacheRegistryUtil.clear(DatiTrasportoImpl.class.getName());
		}

		EntityCacheUtil.clearCache(DatiTrasportoImpl.class.getName());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	/**
	 * Clears the cache for the dati trasporto.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache(DatiTrasporto datiTrasporto) {
		EntityCacheUtil.removeResult(DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
			DatiTrasportoImpl.class, datiTrasporto.getPrimaryKey());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		clearUniqueFindersCache(datiTrasporto);
	}

	@Override
	public void clearCache(List<DatiTrasporto> datiTrasportos) {
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		for (DatiTrasporto datiTrasporto : datiTrasportos) {
			EntityCacheUtil.removeResult(DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
				DatiTrasportoImpl.class, datiTrasporto.getPrimaryKey());

			clearUniqueFindersCache(datiTrasporto);
		}
	}

	protected void cacheUniqueFindersCache(DatiTrasporto datiTrasporto) {
		if (datiTrasporto.isNew()) {
			Object[] args = new Object[] { datiTrasporto.getDocumentoIDFK() };

			FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_DATOTRASPORTO, args,
				Long.valueOf(1));
			FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_DATOTRASPORTO, args,
				datiTrasporto);
		}
		else {
			DatiTrasportoModelImpl datiTrasportoModelImpl = (DatiTrasportoModelImpl)datiTrasporto;

			if ((datiTrasportoModelImpl.getColumnBitmask() &
					FINDER_PATH_FETCH_BY_DATOTRASPORTO.getColumnBitmask()) != 0) {
				Object[] args = new Object[] { datiTrasporto.getDocumentoIDFK() };

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_DATOTRASPORTO,
					args, Long.valueOf(1));
				FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_DATOTRASPORTO,
					args, datiTrasporto);
			}
		}
	}

	protected void clearUniqueFindersCache(DatiTrasporto datiTrasporto) {
		DatiTrasportoModelImpl datiTrasportoModelImpl = (DatiTrasportoModelImpl)datiTrasporto;

		Object[] args = new Object[] { datiTrasporto.getDocumentoIDFK() };

		FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_DATOTRASPORTO, args);
		FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_DATOTRASPORTO, args);

		if ((datiTrasportoModelImpl.getColumnBitmask() &
				FINDER_PATH_FETCH_BY_DATOTRASPORTO.getColumnBitmask()) != 0) {
			args = new Object[] {
					datiTrasportoModelImpl.getOriginalDocumentoIDFK()
				};

			FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_DATOTRASPORTO,
				args);
			FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_DATOTRASPORTO,
				args);
		}
	}

	/**
	 * Creates a new dati trasporto with the primary key. Does not add the dati trasporto to the database.
	 *
	 * @param datiTrasportoId the primary key for the new dati trasporto
	 * @return the new dati trasporto
	 */
	@Override
	public DatiTrasporto create(long datiTrasportoId) {
		DatiTrasporto datiTrasporto = new DatiTrasportoImpl();

		datiTrasporto.setNew(true);
		datiTrasporto.setPrimaryKey(datiTrasportoId);

		return datiTrasporto;
	}

	/**
	 * Removes the dati trasporto with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param datiTrasportoId the primary key of the dati trasporto
	 * @return the dati trasporto that was removed
	 * @throws it.apuliait.NoSuchDatiTrasportoException if a dati trasporto with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public DatiTrasporto remove(long datiTrasportoId)
		throws NoSuchDatiTrasportoException, SystemException {
		return remove((Serializable)datiTrasportoId);
	}

	/**
	 * Removes the dati trasporto with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the dati trasporto
	 * @return the dati trasporto that was removed
	 * @throws it.apuliait.NoSuchDatiTrasportoException if a dati trasporto with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public DatiTrasporto remove(Serializable primaryKey)
		throws NoSuchDatiTrasportoException, SystemException {
		Session session = null;

		try {
			session = openSession();

			DatiTrasporto datiTrasporto = (DatiTrasporto)session.get(DatiTrasportoImpl.class,
					primaryKey);

			if (datiTrasporto == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
				}

				throw new NoSuchDatiTrasportoException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					primaryKey);
			}

			return remove(datiTrasporto);
		}
		catch (NoSuchDatiTrasportoException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	@Override
	protected DatiTrasporto removeImpl(DatiTrasporto datiTrasporto)
		throws SystemException {
		datiTrasporto = toUnwrappedModel(datiTrasporto);

		Session session = null;

		try {
			session = openSession();

			if (!session.contains(datiTrasporto)) {
				datiTrasporto = (DatiTrasporto)session.get(DatiTrasportoImpl.class,
						datiTrasporto.getPrimaryKeyObj());
			}

			if (datiTrasporto != null) {
				session.delete(datiTrasporto);
			}
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		if (datiTrasporto != null) {
			clearCache(datiTrasporto);
		}

		return datiTrasporto;
	}

	@Override
	public DatiTrasporto updateImpl(
		it.apuliait.model.DatiTrasporto datiTrasporto)
		throws SystemException {
		datiTrasporto = toUnwrappedModel(datiTrasporto);

		boolean isNew = datiTrasporto.isNew();

		Session session = null;

		try {
			session = openSession();

			if (datiTrasporto.isNew()) {
				session.save(datiTrasporto);

				datiTrasporto.setNew(false);
			}
			else {
				session.merge(datiTrasporto);
			}
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

		if (isNew || !DatiTrasportoModelImpl.COLUMN_BITMASK_ENABLED) {
			FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
		}

		EntityCacheUtil.putResult(DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
			DatiTrasportoImpl.class, datiTrasporto.getPrimaryKey(),
			datiTrasporto);

		clearUniqueFindersCache(datiTrasporto);
		cacheUniqueFindersCache(datiTrasporto);

		return datiTrasporto;
	}

	protected DatiTrasporto toUnwrappedModel(DatiTrasporto datiTrasporto) {
		if (datiTrasporto instanceof DatiTrasportoImpl) {
			return datiTrasporto;
		}

		DatiTrasportoImpl datiTrasportoImpl = new DatiTrasportoImpl();

		datiTrasportoImpl.setNew(datiTrasporto.isNew());
		datiTrasportoImpl.setPrimaryKey(datiTrasporto.getPrimaryKey());

		datiTrasportoImpl.setDatiTrasportoId(datiTrasporto.getDatiTrasportoId());
		datiTrasportoImpl.setDocumentoIDFK(datiTrasporto.getDocumentoIDFK());
		datiTrasportoImpl.setIdPaese(datiTrasporto.getIdPaese());
		datiTrasportoImpl.setIdCodice(datiTrasporto.getIdCodice());
		datiTrasportoImpl.setCodiceFiscale(datiTrasporto.getCodiceFiscale());
		datiTrasportoImpl.setDenominazione(datiTrasporto.getDenominazione());
		datiTrasportoImpl.setNome(datiTrasporto.getNome());
		datiTrasportoImpl.setCognome(datiTrasporto.getCognome());
		datiTrasportoImpl.setTitolo(datiTrasporto.getTitolo());
		datiTrasportoImpl.setCodeori(datiTrasporto.getCodeori());
		datiTrasportoImpl.setNumeroLicenzaGuida(datiTrasporto.getNumeroLicenzaGuida());
		datiTrasportoImpl.setMezzoTrasporto(datiTrasporto.getMezzoTrasporto());
		datiTrasportoImpl.setNumeroColli(datiTrasporto.getNumeroColli());
		datiTrasportoImpl.setDescrizione(datiTrasporto.getDescrizione());
		datiTrasportoImpl.setUnitaMisuraPeso(datiTrasporto.getUnitaMisuraPeso());
		datiTrasportoImpl.setPesoLordo(datiTrasporto.getPesoLordo());
		datiTrasportoImpl.setPesoNetto(datiTrasporto.getPesoNetto());
		datiTrasportoImpl.setDataOraRitiro(datiTrasporto.getDataOraRitiro());
		datiTrasportoImpl.setDataInizioTrasporto(datiTrasporto.getDataInizioTrasporto());
		datiTrasportoImpl.setTipoResa(datiTrasporto.getTipoResa());
		datiTrasportoImpl.setIndirizzoResa(datiTrasporto.getIndirizzoResa());
		datiTrasportoImpl.setNumeroCivicoResa(datiTrasporto.getNumeroCivicoResa());
		datiTrasportoImpl.setCapResa(datiTrasporto.getCapResa());
		datiTrasportoImpl.setComuneResa(datiTrasporto.getComuneResa());
		datiTrasportoImpl.setProvinciaResa(datiTrasporto.getProvinciaResa());
		datiTrasportoImpl.setNazioneResa(datiTrasporto.getNazioneResa());
		datiTrasportoImpl.setDataOraConsegna(datiTrasporto.getDataOraConsegna());
		datiTrasportoImpl.setNumeroFatturaPrincipale(datiTrasporto.getNumeroFatturaPrincipale());
		datiTrasportoImpl.setDataFatturaPrincipale(datiTrasporto.getDataFatturaPrincipale());

		return datiTrasportoImpl;
	}

	/**
	 * Returns the dati trasporto with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the dati trasporto
	 * @return the dati trasporto
	 * @throws it.apuliait.NoSuchDatiTrasportoException if a dati trasporto with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public DatiTrasporto findByPrimaryKey(Serializable primaryKey)
		throws NoSuchDatiTrasportoException, SystemException {
		DatiTrasporto datiTrasporto = fetchByPrimaryKey(primaryKey);

		if (datiTrasporto == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
			}

			throw new NoSuchDatiTrasportoException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				primaryKey);
		}

		return datiTrasporto;
	}

	/**
	 * Returns the dati trasporto with the primary key or throws a {@link it.apuliait.NoSuchDatiTrasportoException} if it could not be found.
	 *
	 * @param datiTrasportoId the primary key of the dati trasporto
	 * @return the dati trasporto
	 * @throws it.apuliait.NoSuchDatiTrasportoException if a dati trasporto with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public DatiTrasporto findByPrimaryKey(long datiTrasportoId)
		throws NoSuchDatiTrasportoException, SystemException {
		return findByPrimaryKey((Serializable)datiTrasportoId);
	}

	/**
	 * Returns the dati trasporto with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the dati trasporto
	 * @return the dati trasporto, or <code>null</code> if a dati trasporto with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public DatiTrasporto fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		DatiTrasporto datiTrasporto = (DatiTrasporto)EntityCacheUtil.getResult(DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
				DatiTrasportoImpl.class, primaryKey);

		if (datiTrasporto == _nullDatiTrasporto) {
			return null;
		}

		if (datiTrasporto == null) {
			Session session = null;

			try {
				session = openSession();

				datiTrasporto = (DatiTrasporto)session.get(DatiTrasportoImpl.class,
						primaryKey);

				if (datiTrasporto != null) {
					cacheResult(datiTrasporto);
				}
				else {
					EntityCacheUtil.putResult(DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
						DatiTrasportoImpl.class, primaryKey, _nullDatiTrasporto);
				}
			}
			catch (Exception e) {
				EntityCacheUtil.removeResult(DatiTrasportoModelImpl.ENTITY_CACHE_ENABLED,
					DatiTrasportoImpl.class, primaryKey);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return datiTrasporto;
	}

	/**
	 * Returns the dati trasporto with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param datiTrasportoId the primary key of the dati trasporto
	 * @return the dati trasporto, or <code>null</code> if a dati trasporto with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public DatiTrasporto fetchByPrimaryKey(long datiTrasportoId)
		throws SystemException {
		return fetchByPrimaryKey((Serializable)datiTrasportoId);
	}

	/**
	 * Returns all the dati trasportos.
	 *
	 * @return the dati trasportos
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public List<DatiTrasporto> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Returns a range of all the dati trasportos.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link it.apuliait.model.impl.DatiTrasportoModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
	 * </p>
	 *
	 * @param start the lower bound of the range of dati trasportos
	 * @param end the upper bound of the range of dati trasportos (not inclusive)
	 * @return the range of dati trasportos
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public List<DatiTrasporto> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Returns an ordered range of all the dati trasportos.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link it.apuliait.model.impl.DatiTrasportoModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
	 * </p>
	 *
	 * @param start the lower bound of the range of dati trasportos
	 * @param end the upper bound of the range of dati trasportos (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of dati trasportos
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public List<DatiTrasporto> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		boolean pagination = true;
		FinderPath finderPath = null;
		Object[] finderArgs = null;

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			pagination = false;
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
			finderArgs = FINDER_ARGS_EMPTY;
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
			finderArgs = new Object[] { start, end, orderByComparator };
		}

		List<DatiTrasporto> list = (List<DatiTrasporto>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_DATITRASPORTO);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_DATITRASPORTO;

				if (pagination) {
					sql = sql.concat(DatiTrasportoModelImpl.ORDER_BY_JPQL);
				}
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (!pagination) {
					list = (List<DatiTrasporto>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);

					list = new UnmodifiableList<DatiTrasporto>(list);
				}
				else {
					list = (List<DatiTrasporto>)QueryUtil.list(q, getDialect(),
							start, end);
				}

				cacheResult(list);

				FinderCacheUtil.putResult(finderPath, finderArgs, list);
			}
			catch (Exception e) {
				FinderCacheUtil.removeResult(finderPath, finderArgs);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the dati trasportos from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public void removeAll() throws SystemException {
		for (DatiTrasporto datiTrasporto : findAll()) {
			remove(datiTrasporto);
		}
	}

	/**
	 * Returns the number of dati trasportos.
	 *
	 * @return the number of dati trasportos
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public int countAll() throws SystemException {
		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				FINDER_ARGS_EMPTY, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_DATITRASPORTO);

				count = (Long)q.uniqueResult();

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
					FINDER_ARGS_EMPTY, count);
			}
			catch (Exception e) {
				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
					FINDER_ARGS_EMPTY);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the dati trasporto persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.it.apuliait.model.DatiTrasporto")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<DatiTrasporto>> listenersList = new ArrayList<ModelListener<DatiTrasporto>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<DatiTrasporto>)InstanceFactory.newInstance(
							getClassLoader(), listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	public void destroy() {
		EntityCacheUtil.removeCache(DatiTrasportoImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	private static final String _SQL_SELECT_DATITRASPORTO = "SELECT datiTrasporto FROM DatiTrasporto datiTrasporto";
	private static final String _SQL_SELECT_DATITRASPORTO_WHERE = "SELECT datiTrasporto FROM DatiTrasporto datiTrasporto WHERE ";
	private static final String _SQL_COUNT_DATITRASPORTO = "SELECT COUNT(datiTrasporto) FROM DatiTrasporto datiTrasporto";
	private static final String _SQL_COUNT_DATITRASPORTO_WHERE = "SELECT COUNT(datiTrasporto) FROM DatiTrasporto datiTrasporto WHERE ";
	private static final String _ORDER_BY_ENTITY_ALIAS = "datiTrasporto.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No DatiTrasporto exists with the primary key ";
	private static final String _NO_SUCH_ENTITY_WITH_KEY = "No DatiTrasporto exists with the key {";
	private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
				PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
	private static Log _log = LogFactoryUtil.getLog(DatiTrasportoPersistenceImpl.class);
	private static DatiTrasporto _nullDatiTrasporto = new DatiTrasportoImpl() {
			@Override
			public Object clone() {
				return this;
			}

			@Override
			public CacheModel<DatiTrasporto> toCacheModel() {
				return _nullDatiTrasportoCacheModel;
			}
		};

	private static CacheModel<DatiTrasporto> _nullDatiTrasportoCacheModel = new CacheModel<DatiTrasporto>() {
			@Override
			public DatiTrasporto toEntityModel() {
				return _nullDatiTrasporto;
			}
		};
}